package com.emspost.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TimeTest {
    public static void main(String[] args) throws ParseException {
        String dateString = "2025-09-01";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateString, formatter);

        // 计算当前季度和年份 （8-1）/ 3 + 1 = 3
        int currentQuarter = (date.getMonthValue() - 1) / 3 + 1;
        int year = date.getYear();

        // 计算上季度的年份和季度编号
        int lastQuarterYear = (currentQuarter == 1) ? year - 1 : year;
        int lastQuarter = (currentQuarter == 1) ? 4 : currentQuarter - 1;
        // 计算上季度的起始月份
        Month startMonth = Month.of((lastQuarter - 1) * 3 + 1);
        // 获取上季度的第一天和最后一天
        LocalDate firstDay = LocalDate.of(lastQuarterYear, startMonth, 1);
        LocalDate lastDay = firstDay.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());

        System.out.println("上季度开始: " + firstDay);
        System.out.println("上季度结束: " + lastDay);


    }
}
